<?php

class Activity extends CI_Controller {

    function __construct() {
        parent::__construct();
//cargamos el odelo para que todas las funciones las usen si es necesario
        $this->load->model('_activity');
        $this->load->library('form_validation');
    }

    public function add($id = null) {
        $edit = false;
        $datos = array();
        if (isset($id)) {
            $edit = true;
            $datos = $this->_activity->getActivity($id);
        }
        if (!$this->input->post() || $edit) {


            if (!$edit) {
                $datos['id'] = "";
                $datos['name'] = "";
                $datos['placement'] = "";
                $datos['description'] = "";
                $datos['price'] = "";
                $datos['date_act'] = "";

                $datos['title'] = "ADD";
            } else {
                $datos['title'] = "EDIT";
            }

            $template_data['title'] = $datos['title'] . ' ACTIVITY';
            $template_data['menu'] = $this->cargar_menu();
            $template_data['content'] = $this->load->view('activity/add', $datos, TRUE);

            $this->load->view('templates/template', $template_data);
        } else {

            $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');

            $this->form_validation->set_rules('placement', 'Placement', 'trim|required|xss_clean');

            $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');

            $this->form_validation->set_rules('price', 'Price', 'trim|required|xss_clean');

            $this->form_validation->set_rules('date_act', 'Date', 'trim|required|xss_clean');

            if ($this->form_validation->run() == true) {


                $post = $this->input->post();
                $datos = array();
                $datos['id'] =$post['id'];
                $datos['name'] = $post['name'];;
                $datos['placement'] = $post['placement'];;
                $datos['description'] = $post['description'];;
                $datos['price'] = $post['price'];;
                $datos['date_act'] = $post['date_act'];;
                if ($post['submit'] == 'Add') {
                    $this->_activity->add_activity($datos);
                } else {
                    $this->_activity->save_activity($datos);
                }

                $this->form_validation->set_message('register', 'Register Succeed');

                $sess_array = array('type' => 'good', 'text' => 'Register Succeed');
                $this->session->set_userdata('notification', $sess_array);
                redirect('/activity/show', 'refresh');
            } else {


                $sess_array = array('type' => 'bad', 'text' => validation_errors());
                $this->session->set_userdata('notification', $sess_array);
                $template_data['title'] = "ADD";
                $template_data['menu'] = $this->cargar_menu();
                $template_data['content'] = $this->load->view('activity/add', array(), true);
                $this->load->view('templates/template', $template_data);
            }
        }
    }

    public function list_activities() {

        $this->_activity->list_activities();
    }

    public function show($pag = 1) {
        $user_type = 0;
        $ac_por_pag = 5;
        $qtd_activity = $this->_activity->count_activities();
        

        $num_pag = intval($qtd_activity / $ac_por_pag) + 1;


        if($qtd_activity!=$ac_por_pag){
            $num_pag++;
        }
        
        $min = ($pag - 1) * $ac_por_pag;
        $max = $ac_por_pag;
        if ($num_pag >= 1) {
            $pager = $this->load->view('templates/pager', array("pag" => $pag, "num_pag" => $num_pag), TRUE);
        } else {
            $pager = "";
        }


        if (isset($this->session->userdata["logged_in"])) {//si hay una sesion definida se llama a la lista de actvidades con los botones de apuntarse a la activdad
            $user_type = $this->session->userdata["logged_in"]["group"];
            $id_user = $this->session->userdata["logged_in"]["id"];

            $this->load->model('user_activity');
            $activities = $this->user_activity->get_Activity($id_user);




            $template_data['menu'] = $this->cargar_menu();



            $template_data['content'] = $this->load->view('activity/show', array("list" => $this->_activity->list_activities($min, $max), "ua" => $activities, "user_type" => $user_type, "pager" => $pager), TRUE);



            $this->load->view('templates/template', $template_data);
        } else {//si no, se llama a la lista de actividades sin poder apuntarte a ella
            $template_data['menu'] = $this->cargar_menu();
            $template_data['content'] = $this->load->view('activity/show', array("list" => $this->_activity->list_activities($min, $max), "ua" => array(), "user_type" => $user_type, "pager" => $pager), TRUE);
            $this->load->view('templates/template', $template_data);
        }
    }

    public function cargar_menu() {

        if (!isset($this->session->userdata["logged_in"])) {//si no esta logeado se carga el menu common/menu
            return($this->load->view('common/menu', array(), TRUE));
        }
        if ($this->session->userdata["logged_in"]["group"] == "2") {

            return $this->load->view('user/menu', array(), TRUE);
        }
        if ($this->session->userdata["logged_in"]["group"] == "1") {


            return $this->load->view('admin/menu', array(), TRUE);
        }
    }

    public function delete($id) {

        $this->_activity->delete($id);

        $sess_array = array('type' => 'good', 'text' => "Deleted Successful");
        $this->session->set_userdata('notification', $sess_array);
        redirect('/activity/show', 'refresh');
    }

    /* public function add(){

      //recupera los datos del form

      $name = $_POST['name'];
      $date = $_POST['date'];
      $url = $_POST['url'];
      $price = $_POST['price'];

      // carga el modelo
      $this->load->model('Activity','',TRUE);

      //llama la funcion para a�adir la nueva actividad en la base de datos, pasando los parametros
      $this->Activity->addActivity($name,$date,$url,$price);

      //redireciona para la pagina de lista de Actividades
      redirect('Activity/view/ListaAtividades','refresh');

      } */
}